//
//  GLUploadProgressView.m
//  MaMiSchool
//
//  Created by huanggulong on 16/7/23.
//  Copyright © 2016年 历山大亚. All rights reserved.
//

#import "GLUploadProgressView.h"

@interface GLUploadProgressView()


@property(nonatomic , strong)UIView *  contentView;

@property(nonatomic , weak)UILabel *  progressLabel;

@property(nonatomic , strong)CAShapeLayer *  backLayer;

@property(nonatomic , strong)CAShapeLayer *  progressLayer;

@end

@implementation GLUploadProgressView


- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView *progressView = [[UIView alloc] initWithFrame:CGRectMake((self.width - 125) * 0.5, (self.height - 98) * 0.5, 125, 98)];
        progressView.layer.cornerRadius = 8;
        progressView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        progressView.layer.masksToBounds = YES;
        [self addSubview:progressView];
        _contentView = progressView;
        
        
//        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 80, 25)];
//        label1.text = @"上传中...";
//        label1.textAlignment = NSTextAlignmentRight;
//        label1.font = [UIFont systemFontOfSize:15];
//        label1.textColor = [UIColor whiteColor];
//        [progressView addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 125, 25)];
        label2.text = @"0%";
        label2.font = [UIFont systemFontOfSize:15];
        label2.textColor = [UIColor whiteColor];
        label2.textAlignment = NSTextAlignmentCenter;
        [progressView addSubview:label2];
        _progressLabel = label2;
        
        UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, 45, 25)];
        label3.text = @"1/1";
        label3.font = [UIFont systemFontOfSize:15];
        label3.textColor = [UIColor whiteColor];
        label3.textAlignment = NSTextAlignmentCenter;
        [progressView addSubview:label3];
        _countLabel = label3;
        
        CAShapeLayer *layer1 = [CAShapeLayer new];
        layer1.fillColor = nil;
        layer1.frame = progressView.bounds;
        [progressView.layer addSublayer:layer1];
        layer1.lineWidth = 4;
        layer1.strokeColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
        _backLayer = layer1;
        
        CAShapeLayer *layer2 = [CAShapeLayer new];
        layer2.fillColor = nil;
        layer2.frame = progressView.bounds;
        [progressView.layer addSublayer:layer2];
        layer2.lineWidth = 4;
        layer2.lineCap = kCALineCapRound;
        layer2.strokeColor = [UIColor whiteColor].CGColor;
        _progressLayer = layer2;
        
        [self setTrack];
        [self setLayerProgress:0.0];
    }
    return self;
}


- (void)setTrack
{
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(_contentView.width * 0.5, _contentView.height * 0.5) radius:40 startAngle:0 endAngle:M_PI * 2 clockwise:YES];;
    _backLayer.path = path1.CGPath;
}

- (void)setLayerProgress:(CGFloat)progress
{
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(_contentView.width * 0.5, _contentView.height * 0.5) radius:40 startAngle:- M_PI_2 endAngle:(M_PI * 2) * progress - M_PI_2 clockwise:YES];;
    _backLayer.path = path1.CGPath;
}

+ (GLUploadProgressView *)showInView:(UIView *)view{
    GLUploadProgressView *pview = [[GLUploadProgressView alloc] initWithFrame:view.bounds];
    NSProgress *progress = [[NSProgress alloc] init];
    pview -> _progress = progress;
    [progress addObserver:pview forKeyPath:@"completedUnitCount" options:(NSKeyValueObservingOptionNew) context:nil];
    [view addSubview:pview];
    
    return pview;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"completedUnitCount"]) { //主界面中修改视图
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_progress.completedUnitCount >= _progress.totalUnitCount) {
                _progressLabel.text = @"100%";
            }else{
                if (_progress.totalUnitCount != 0) {
                    _progressLabel.text = [NSString stringWithFormat:@"%lld%%",_progress.completedUnitCount * 100 / _progress.totalUnitCount];
                    [self setLayerProgress:_progress.completedUnitCount * 1.0/_progress.totalUnitCount];
                }else{
                    _progressLabel.text = @"0%";
                }
            }
        });
    }
}

- (void)dealloc{
    [_progress removeObserver:self forKeyPath:@"completedUnitCount"];
}

@end
